<template>
  <div>
    <el-dialog
      title="房态调整"
      :visible.sync="dialogVisible"
      width="650px"
      :close-on-click-modal="false"
      @close="close"
    >
      <el-form
        ref="form"
        v-loading="loading"
        :model="form"
        :rules="rules"
        label-suffix="："
        label-width="100px"
        size="small"
      >
        <el-form-item label="房型">
          <div v-for="(item,index) in productList" :key="index">
            <div>{{ item.productName }}</div>
          </div>
        </el-form-item>
        <el-form-item label="日期" prop="days">
          <el-date-picker
            v-model="form.days"
            size="small"
            value-format="yyyyMMdd"
            :picker-options="pickerOptions"
            format="yyyy-MM-dd"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio v-model="form.state" :label="1">可预订</el-radio>
          <el-radio v-model="form.state" :label="0">已满房</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" round :disabled="loading" @click="hide">取 消</el-button>
        <el-button size="small" round :disabled="loading" type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { modifyProductState } from '../../../api/product'
export default {
  data() {
    return {
      /**
         * 校验字段相关
         */
      rules: {
        days: [
          {
            required: true,
            message: '请选择日期'
          }
        ],
        state: [
          {
            message: '请选择状态',
            required: true
          }
        ]
      },
      // 配置
      pickerOptions: {
        // 筛选日期
        disabledDate(time) {
          return time.getTime() < Date.now() - 86400000
        }
      },
      resolve: null,
      reject: null,
      // 状态组
      loading: false,
      dialogVisible: null,
      // 数据
      productList: [],
      form: {
        days: '',
        shopId: '',
        productIds: [],
        state: 1
      }
    }
  },
  methods: {
    open({ shopId, productList }) {
      this.form.shopId = shopId
      this.productList = productList
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.loading = false
      this.form = {
        days: '',
        shopId: '',
        productIds: [],
        state: 1
      }
      this.$refs.form.resetFields()
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    /**
       * 按钮组
       */
    // 保存
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(this.form))
          const days = []
          // 获取日期
          while (
            this.$momentMini(params.days[1]).unix() -
                this.$momentMini(params.days[0]).unix() >=
              0
          ) {
            days.push(params.days[0])
            params.days[0] = this.$momentMini(params.days[0])
              .add(1, 'days')
              .format('YYYYMMDD')
          }
          // 设置id
          this.productList.forEach(v => {
            params.productIds.push(v.productId)
          })
          params.days = days
          this.loading = true
          modifyProductState(params)
            .then(res => {
              this.$notify({
                title: '成功',
                message: '保存成功',
                type: 'success',
                duration: 2000,
                offset: 40
              })
              this.resolve && this.resolve()
              this.hide()
            })
            .catch(() => {})
            .finally(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
